package com.ffyc.ssm.controller;

import com.ffyc.ssm.common.CommonResult;
import com.ffyc.ssm.dao.AdminDao;
import com.ffyc.ssm.modle.Admin;
import com.ffyc.ssm.modle.Role;
import com.ffyc.ssm.service.AdminService;
import com.ffyc.ssm.util.LoginInterceptor;
import com.ffyc.ssm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(path = "/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @PostMapping(path = "/fileUpload")
    public  CommonResult fileUpload(@RequestParam("fileName")CommonsMultipartFile file, HttpSession session){
        CommonResult commonResult =null;
        Admin admin =(Admin) session.getAttribute("admin");
        File f0 =new File("D:\\UploadImg\\"+admin.getAccount());
        if (!f0.exists()){
        f0.mkdir();
        }
        String oldFileName= file.getOriginalFilename();
        String newFileName =StringUtil.newFileName(oldFileName);
        File f =new File(f0,newFileName);
        try {
            file.transferTo(f);
            Admin a=new Admin();
            a.setId(admin.getId());
            a.setNewFileName(newFileName);
            a.setOldFileName(oldFileName);
            adminService.updateAdmin(a);
            commonResult=new CommonResult(200,"上传成功",a);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"上传失败",null);
        }
        return  commonResult;
    }

    /**
     * 查询管理列表
     * @param admin
     * @param session
     * @return
     */
    @GetMapping(path = "/Admin")
    public CommonResult login(Admin admin,HttpSession session){
        CommonResult<List> commonResult = null;
        try {
            List<Admin> list = adminService.listAdmin(admin);
            if (list!=null){
                session.setAttribute("admin",list);
                commonResult = new CommonResult<>(200,"登录成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }

    /*查找角色列表*/
    @GetMapping(path = "/roles")
    public CommonResult roles(){
        CommonResult commonResult = null;
        try {
           List<Role> roles =adminService.getroleslist();
            if (roles!=null){
                commonResult = new CommonResult(200,"登录成功" , roles);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }

    //保存管理员
    @RequestMapping(path = "/save")
    public CommonResult save(Admin admin){
        CommonResult commonResult = null;
        try {
            System.out.println("id:::::::::::::::"+admin.getId());
            if (admin.getId()==null){
                System.out.println("=======id为空======saveadmin");
                adminService.saveAdmin(admin);
            }else {
                System.out.println("=====id不为空====upsaveAdmin");
                adminService.upsaveAdmin(admin);
            }
           // adminService.saveAdmin(admin);
            commonResult = new CommonResult(200,"修改保存成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }
    //查询管理员
    @GetMapping(path = "/findAdmin")
    public CommonResult findAdmin(Integer id){
        CommonResult commonResult = null;
        try {
        Admin admin =adminService.findAdmin(id);
                commonResult = new CommonResult(200,"登录成功" ,admin );

        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }
    @GetMapping(path = "/delete")
    public  CommonResult delete(Integer id){
        CommonResult<Admin> commonResult = null;
        try {
            adminService.delete(id);
            commonResult = new CommonResult<Admin>(200,"删除成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<Admin>(500,"登录失败" , null);
        }
        return commonResult;
    }

    @PostMapping(path = "/update")
    public CommonResult update(@RequestParam("account")String account,@RequestParam("rolesId")String rolesId,@RequestParam("id")String id){
        CommonResult commonResult = null;
        try {
            Map<String,String> map = new HashMap();
            map.put("account",account);
            map.put("rolesId",rolesId);
            map.put("id",id);
            adminService.update(map);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }
}
